package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.fhb.itunes.manager.filewatcher.OrdnerUebweachung;
import de.fhb.itunes.manager.mail.Mail;
import de.fhb.itunes.manager.mail.Template.Template;
import de.fhb.itunes.manager.mail.Template.TemplateBuilder;
import de.fhb.itunes.repository.DataAccess.DAO_Film;
import de.fhb.itunes.repository.data.Film_VO;

/**
 * Servlet implementation class update
 */
@WebServlet("/update")
public class update extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public update() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			if (objtmp != null) {
				boolean isLoggedIn = (Boolean) objtmp;
				if (isLoggedIn) {// session.isNew()
					nextJSP = "/update.jsp";
					Enumeration en = request.getParameterNames();
					int modus = -1;
					while (en.hasMoreElements()) {
						String paramName = (String) en.nextElement();
						String paraValue = request.getParameter(paramName);
						if (paramName.contains("mode")) {
							modus = Integer.parseInt(paraValue);
						}
					}
					if (modus != -1) {
						// daten in automatisch zu itunes hinzufügen kopieren
						// und emails verschicken

						OrdnerUebweachung ordner = new OrdnerUebweachung();
						try {
							ordner.checkDir();
							TemplateBuilder tb = new TemplateBuilder(
									ordner.getFilmeNames(),
									ordner.getMusikNames());
							request.setAttribute("templates", tb.getTemplates());
							nextJSP = "/update.jsp";

						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						// request.setAttribute("update", true);
					}
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			if (objtmp != null) {
				boolean isLoggedIn = (Boolean) objtmp;
				if (isLoggedIn) {// session.isNew()
					// logik anfang
					nextJSP = "/update.jsp";
					
					Enumeration en = request.getParameterNames();
					String keyword = "";
					List<String> titelList = new LinkedList<String>();
					List<String> texte = new LinkedList<String>();
					List<String> urls = new LinkedList<String>();
					while (en.hasMoreElements()) {
						String paramName = (String) en.nextElement();
						String paraValue = request.getParameter(paramName);
						if (paramName.contains("txt_")) {
							texte.add(paraValue);
						} else if(paramName.contains("url_")){
							urls.add(paraValue);
						}
						else if(paramName.contains("titel_")){
							titelList.add(paraValue);
						}
					}
					
					OrdnerUebweachung ordner = new OrdnerUebweachung();
					try {
						Template standard = new Template("Es gibt gute Neuigkeiten!!! Es wurde neue Titel zu Ihrer Mediathek hinzugefügt");
						List<Template> listOfTemps = new ArrayList<Template>();
						int i= 0;
						for (String titel : titelList) {
							listOfTemps.add(new Template(texte.get(i),titel,urls.get(i)));
							i++;
						}
						
						
						
						Mail mail = new Mail();
						mail.addTemplate(standard);
						boolean neuedateien = false;
						for (Template template : listOfTemps) {
							mail.addTemplate(template);
							neuedateien = true;
						}
						if(neuedateien)
							mail.send();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					nextJSP = "/index.jsp";
					// logik ende
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

}
